﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using GameServerApp.DBModel;
using System.Diagnostics;
using GameServerApp.Controller;
using GameServerApp.PVP.WorldMap;

namespace GameServerApp
{
    class Program
    {
        private static string m_ServerIP = "192.168.31.90";
        private static int m_Port = 1012;

        private static Socket m_ServerSocket;

        /// <summary>
        /// 初始化所有控制器
        /// </summary>
        static void InitAllController()
        {
            //角色初始化
            RoleController.Instance.Init();

            //世界地图场景管理器初始化
            WorldMapSceneMgr.Instance.Init();
        }


        static void Main(string[] args)
        {
            DataTableManager.Instance.LoadDataTable();

            InitAllController();

            //实例化服务器Socket
            m_ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //向操作系统申请一个可用的IP和端口用来通讯
            m_ServerSocket.Bind(new IPEndPoint(IPAddress.Parse(m_ServerIP), m_Port));

            //设置最多3000个排队连接请求
            m_ServerSocket.Listen(3000);

            Console.WriteLine("启动监听{0}成功   {1}", m_ServerSocket.LocalEndPoint.ToString(), DateTime.Now);

            Thread mThread = new Thread(ListenClientCallBack);
            mThread.Start();

            //MailDBModel.Instance.Init();

            //Console.WriteLine(DBConn.DBGameServer);


            //=============================================
            ////添加注册到数据库
            //RoleEntity entity = new RoleEntity();
            //entity.NickName = "张三";
            //entity.JobId = 1;
            //entity.CreatTime = DateTime.Now;
            //entity.UpdateTime = DateTime.Now;
            //entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released;

            //RoleCacheModel.Instance.Create(entity);

            ////修改数据库1
            //RoleEntity entity = new RoleEntity();
            //entity.Id = 3;
            //entity.NickName = "李四";
            //entity.JobId = 1;
            //entity.CreatTime = DateTime.Now;
            //entity.UpdateTime = DateTime.Now;
            //entity.Status = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released;
            //RoleCacheModel.Instance.Update(entity);

            ////修改数据库2 做参数的时候不能有[]  [@NickName]
            //Dictionary<string, object> dic = new Dictionary<string, object>();
            //dic["@NickName"] = "荒芜";
            //dic["@Id"] = 3;
            //RoleCacheModel.Instance.Update("NickName=@NickName", "Id=@Id", dic);

            ////查询
            //RoleEntity entity = RoleCacheModel.Instance.GetEntity(3);
            //Console.WriteLine(entity.NickName);

            ////删除(假删除, 方便角色找回)
            //RoleCacheModel.Instance.Delete(3);

            //=============================================
            Console.ReadLine();



        }

        /// <summary>
        /// 监听客户端连接
        /// </summary>
        private static void ListenClientCallBack()
        {

            while (true)
            {

                //接收客户端的请求
                Socket socket = m_ServerSocket.Accept();

                Console.WriteLine("客户端{0}已经连接    {1}", socket.RemoteEndPoint.ToString(), DateTime.Now);


                if (((IPEndPoint)socket.RemoteEndPoint).Address.ToString() == "192.168.31.90")
                {
                    RechargeCtrl.Instance.Recharge(socket);
                    Console.WriteLine("客户端充值");
                }
                else
                {

                    //一个角色 就相当于一个客户端
                    Role role = new Role();
                    ClientSocket clientSocket = new ClientSocket(socket, role);

                    //把角色添加到角色管理
                    RoleMgr.Instance.AllRole.Add(role);
                }

                ////一个角色就相当于一个客户端
                //Role role = new Role();
                //ClientSocket clientSocket = new ClientSocket(socket, role);

                ////把角色添加到角色管理
                //RoleMgr.Instance.AllRole.Add(role);

            }


        }





    }
}
